package com.woniuxy.dr_mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.dr_mall.entity.*;
import com.woniuxy.dr_mall.global.ResponseStateCode;
import com.woniuxy.dr_mall.service.IDrSpecPriceService;
import com.woniuxy.dr_mall.utils.SpecUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张哲轩
 * @since 2021-08-11
 */
@RestController
@Slf4j
@RequestMapping("/dr_mall/drSpecPrice")
public class DrSpecPriceController {

    @Autowired
    private IDrSpecPriceService specPriceService;

    @GetMapping("getPriceByPid")
    public MyResponseEntity<List<DrSpecPrice>> getPriceByPid(int pid) {
        return new MyResponseEntity<List<DrSpecPrice>>(specPriceService.getPriceByPid(pid));
    }

    @PostMapping("get_skus")
    public MyResponseEntity<List<DrSpecPrice>> getSkus(@RequestBody DrSpecVO specVO) {
        List<DrSpecPrice> l = specPriceService.getSkus(specVO);
        return new MyResponseEntity<>(specPriceService.getSkus(specVO));
    }

    /**
     * @return com.woniuxy.dr_mall.entity.MyResponseEntity<com.woniuxy.dr_mall.entity.DrProducts>
     * @description:得到所有sku视图列表，用于后台展示
     * @Author 张哲轩
     * @Date 2021/8/16 11:42
     */
    @GetMapping("getAllSkus")
    public MyResponseEntity<List<DrVSku>> getAllProducts() {
        List<DrVSku> l = specPriceService.getAllVSku();
        for (DrVSku drVSku : l) {
            String path = drVSku.getPictures().get(0).getImage();
            drVSku.getPictures().get(0).setImage("http://localhost:8080/"+path);
        }
        return new MyResponseEntity<>(l);
    }

    /**
     * @return com.woniuxy.dr_mall.entity.MyResponseEntity<?>
     * @description: 增加sku信息
     * @Author 张哲轩
     * @Date 2021/8/19 15:44
     */
    @PostMapping("add_sku")
    public MyResponseEntity<?> addSku(@RequestBody AddProduct addProduct) {
        QueryWrapper<DrSpecPrice> wrapper = new QueryWrapper<>();
        wrapper.eq("productId",addProduct.getProductId());
        for (DrSpecEntry specEntry : addProduct.getSpecEntry()) {
            wrapper.eq(specEntry.getName(),specEntry.getSpecValId());
        }
        DrSpecPrice oldSpecPrice= specPriceService.getOne(wrapper);
        //已存在该sku
        if(oldSpecPrice!=null){
            DrSpecPrice newSku=new DrSpecPrice();
            newSku.setId(oldSpecPrice.getId());
            if(addProduct.getPrice()!=null){
                newSku.setPrice(addProduct.getPrice());
            }
            newSku.setStock(oldSpecPrice.getStock()+addProduct.getStock());
            specPriceService.updateById(newSku);
            return new MyResponseEntity<>(ResponseStateCode.SKU_DUPLICATE,"sku存在");
        }
        DrSpecPrice specPrice =new DrSpecPrice();
        specPrice.setStock(addProduct.getStock());
        specPrice.setPrice(addProduct.getPrice());
        SpecUtil.addSkuSpecs(addProduct, specPrice);
        specPrice.setProductId(addProduct.getProductId());
        specPriceService.save(specPrice);
        return MyResponseEntity.SUCCESS;
    }


}

